﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Tour.BLL;
using Tour.DAL;

public partial class Admin_Modules_Event_EventAE : VST.Controls.BaseLCGPage
{

    public int? EventId { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        EventId = CommonHelper.QueryStringInt("EventId", null);
        //if (!EventId.HasValue)
        //    Response.Redirect("EventList.aspx");
        if (!IsPostBack)
        {
            LoadData();
            LoadDDL();
        }
    }
    void LoadData()
    {
        if (EventId.HasValue)
        {
            TblEventEntity item = TblEventManager.CreateInstant().SelectOne(EventId.Value);
            txtName.Text = item.Name;
            cbIsVisible.Checked = item.IsVisible;
            hdfimgImageTour.Value = item.Image;
            radEditorContent.Content = item.Description;
            ddlEventType.SelectedValue = item.EventTypeId.ToString();
        }
        //else
        //{
        //    txtCreateDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        //    lblCreateTime.Text = DateTime.Now.ToString("HH:mm:ss");
        //    LiteralUpdatedDate.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
        //}
    }
    void LoadDDL()
    {
        List<TblEventTypeEntity> ls = TblEventTypeManager.CreateInstant().SelectTblEventTypeBy(null, WorkingLanguage, "", true, "Id", "DESC");
        ddlEventType.DataSource = ls;
        ddlEventType.DataTextField = "Name";
        ddlEventType.DataValueField = "Id";
        ddlEventType.DataBind();
    }
    TblEventEntity GetTblEvent()
    {
        TblEventEntity item = new TblEventEntity();
        if (EventId.HasValue)
        {
            item = TblEventManager.CreateInstant().SelectOne(EventId.Value);
        }
        else
        {
            item.Id = 0;
            item.TextId = TblEventManager.CreateInstant().GetUniqueTextIdFromUnicodeText(txtName.Text);
            item.DisplayOrder = 50;
            item.CreatedBy = HttpContext.Current.User.Identity.Name;
            item.CreatedDate = DateTime.Now;
        }
        item.LanguageId = WorkingLanguage;
        item.UpdatedBy = HttpContext.Current.User.Identity.Name;
        item.UpdatedDate = DateTime.Now;
        item.IsVisible = cbIsVisible.Checked;
        item.Image = hdfimgImageTour.Value;
        item.Name = txtName.Text;
        item.EventTypeId = ddlEventType.SelectedValue.ToInt();
        item.Description = radEditorContent.Content;

        return item;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        TblEventManager.CreateInstant().InsertUpdate(GetTblEvent());
        Response.Redirect("EventList.aspx?EventId=" + EventId);
    }
}
